package utils;

import java.io.IOException;
import java.text.MessageFormat;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Pager extends SimpleTagSupport {
	private int curpage;	//当前页
	private int pageSize;   //页大小
	private int total;		//总记录数
	private int pages;		//总页数
	private String url;		//请求路径(页面)
	private String param;	//请求参数
	private int pageIndex; 	//显示页数，比如 [1] [2] [3] [4],默认显示9个
	
	public Pager() {
		this.pageIndex = 9;
		this.param = ""; 
	}
	
	@Override
	public void doTag() throws JspException, IOException {
		String script = getScript(); // js
		String style = getStyle();	// 样式
		
		StringBuffer html = new StringBuffer("<ul class='pageUl'>" + lastPage()
				+ nextPage()+ itemPage() + prevPage() + firstPage() +titlePage()+ "</ul>" + script + style);
		
		this.getJspContext().getOut().print(html.toString());
	}
	
	private String titlePage() {
		String str="<li class='title_li'>总记录数：<b class='title_b'>"+total+"</b>&nbsp;条  总页数：<b class='title_b'>"+pages+"</b>&nbsp;页  每页显示条数：<input type='text' title='输入完成按下回车键查询' id='u_size' class='input_text' value="+this.pageSize+" "+" onkeydown='skipPageSize(event,"+this.total+","+this.curpage+","+this.pageSize+",this)'/>&nbsp;条  当前第&nbsp;<input type='text' title='输入页码,按回车快速跳转' value="+this.curpage+" " +
				"id='u_in' class='input_text' " +
				"onkeydown='keyDowns(event,"+this.pages+",this)' />  /"+this.pages+"&nbsp;页</li> ";
		return str;
	}
	
	/**
	 * 首页
	 * @return
	 */
	private String firstPage() {
		String page = "";
		String showName = "1...";
		
		if(this.curpage <= 1) {
			page = getNullLink(showName);
		} else {
			page = getFPNLlink(1, showName);
		}
		return page;
	}
	
	/**
	 * 上一页
	 * @return
	 */
	private String prevPage() {
		String page = "";
		String showName = "<<";
		
		if(this.curpage <= 1) {
			page = getNullLink(showName);
		} else {
			page = getFPNLlink(this.curpage - 1, showName);
		}
		return page;
	}
	
	/**
	 * 下一页
	 */
	private String nextPage() {
		String page = "";
		String showName = ">>";
		
		if(this.curpage >= this.pages) {
			page = getNullLink(showName);
		} else {
			page = getFPNLlink(this.curpage + 1, showName);
		}
		return page;
	}
	
	/**
	 * 末页
	 */
	private String lastPage() {
		String page = "";
		String showName = "..." + this.pages;
		
		if(this.curpage >= this.pages) {
			page = getNullLink(showName);
		} else {
			page = getFPNLlink(this.pages, showName);
		}
		return page;
	}
	
	/**
	 * 中间数字连接
	 * @return
	 */
	private String itemPage() {
		String i_page = "";
		int index = 0;		//显示页数，比如 [1] [2] [3] [4]
		int variable = this.curpage;	//保存当前页变量,主要作用判断是否是前4页和后4页
		int fristPages = this.pageIndex / 2;	
		int totalPages = this.pageIndex - 1; //数字显示的总数,显示几个数字
		
		if(variable > fristPages && variable < this.pages - fristPages) {
			variable = variable - fristPages;
		} else if(variable + fristPages >= this.pages && this.pages > totalPages) {
			variable = this.pages - totalPages;
		} else {
			variable = 1;
		}
		for(int i = variable; i <= this.pages; i++) {
			if( i == this.curpage) {
				i_page = getCurpageLink(i)+i_page ;
			} else {
				i_page= getNumberLink(i)+i_page;
			}
			index++;
			if(index == this.pageIndex) {
				break;
			}
		}
		return i_page;
	}
	
	/**
	 * 上一页,下一页,首页,末页 超链接格式
	 * showName 页面显示文字
	 * page 页数
	 * @return 格式化后的链接地址
	 */
	private String getFPNLlink(int page, String showName) {
		String link = "<a href='javascript:void(0);' url=\"{0}?curpage={1}{2}&flag=2&pageSize={4}\" class='linkpage'><li class='fpnl_li' " +
				"onmousemove='overBorder(this)' onmouseout='outBorder(this)'>{3}</li></a>";
		
		link = MessageFormat.format(link, this.url, page + "", this.param, showName,this.pageSize);
		
		return link;
	}
	
	/**
	 * 中间数字超链接格式
	 * number 当前显示页数
	 * @return 格式化后的链接地址
	 */
	private String getNumberLink(int number) {
		String link = "<a href='javascript:void(0);' url=\"{0}?curpage={1}{2}&flag=2&pageSize={4}\" class='linkpage'><li class='item_li' " +
				"onmousemove='overBorder(this)' onmouseout='outBorder(this)'>{3}</li></a>";
		
		link = MessageFormat.format(link, this.url,number+"",this.param,  number+"",this.pageSize);
		
		return link;
	}
	
	/**
	 * 当前页是第一页和最后页的格式.(去掉超链接)
	 * showName 页面显示文字
	 * @return 格式化后的链接地址
	 */
	private String getNullLink(String showName) {
		String link = "<a href=\"javascript:void(0);\"><li class='fpnl_li' onmousemove='overBorder(this)' " +
				"onmouseout='outBorder(this)'>{0}</li></a>";
		
		link = MessageFormat.format(link, showName);
		
		return link;
	}
	
	/**
	 * 当前页超链接格式
	 * number 当前显示页数
	 * @return 格式化后的链接地址
	 */
	private String getCurpageLink(int number) {
	    String link = "<li class='item_li' style=\"background-color:#E8E8FF;font-weight:bold;\">"+number+""+"</li>";
	   
		return link;
	}
	
//	/**
//	 * 输入框
//	 */
//	private String getUserInput() {
//		String link = "<li class='user_in_li' onmousemove='overBorder(this)' onmouseout='outBorder(this)'>" +
//					"<input type='text' title='输入页码,按回车快速跳转' value="+this.curpage+" " +
//					"id='u_in' class='input_text' " +
//					"onkeydown='keyDowns(event,"+this.pages+")' /> / "+this.pages+"</li>";
//		return link;
//	}
	
	/**
	 * 该分页标签的样式
	 * @return
	 */
	private String getStyle() {
		String style = "<style>" +
		".pageUl{list-style:none;height:40px;padding:0px;margin:0px;font-size:12px;clear:both;}.pageUl a{cursor:pointer;}" +
		".item_li{float:right;border:1px solid #C4C4FF;padding:5px;width:22px;text-align:center;margin-left:3px;}" +
		".title_li{float:right;margin-right:20px;line-height:30px;}"+
		".title_b{color:blue;}"+
		".fpnl_li{float:right;border:1px solid #C4C4FF;padding:5px;width:40px;text-align:center;margin-left:3px;}" +
		".total_li{float:right;font-size:12px;width:60px;padding:5px;}" +
		".user_in_li{float:right;border:1px solid #C4C4FF;width:90px;padding:2px;height:25px;text-align:center;margin-left:3px;}" +
		".input_text{width:30px;border-top:1px solid #999999;border-left:1px solid #999999;" +
		"border-right:1px solid #E4E4E4;border-bottom:1px solid #E4E4E4;text-align:center;}" +
		"#u_in ,#u_size{height:13px;margin-top:5px;}" +
		"</style>";
		
		return style;
	}
	
	/**
	 * 该分页标签的js
	 */
	private String getScript() {
		String script = "<script type='text/javascript'>$(function() { $('.linkpage').die().live('click',function() {$.get($(this).attr('url'),function(v) { $('.packagepages').html(v);loadCourierInfo(); });}); });</script><script>function keyDowns(e,pages,anode) {" +
		"var ev = window.event || e;var code = ev.keyCode || ev.which;" +
		"if(code == 13) {var num = $(anode).val();" +
		"if(num > pages) {num = 1;} else if(num < 1) {num = 1;}" +
		"if(num != null && num != '' && Number(num))" +
		"{$.get(\""+url+"?curpage=\"+num+\"&pageSize="+pageSize+param+"\",function(v) {$('.packagepages').html(v);loadCourierInfo();}); }}}" +
		"function overBorder(tag) {tag.style.border = '1px solid #7575FF';}" +
		"function outBorder(tag) {tag.style.border = '1px solid #C4C4FF';}" +
		"function skipPageSize(e,total,curpage,pageSize,anode) {var ev = window.event || e;var code = ev.keyCode || ev.which;if(code == 13) {var num = $(anode).val();if(num > total) {num = total;} else if(num < 1) {num = 1;}" +
		"if(num != null && num != '' && Number(num)) {$.get(\""+url+"?curpage=\"+curpage+\"&pageSize=\"+num+\""+param+"\",function(v) {$('.packagepages').html(v);loadCourierInfo();}); }}}" +
		"</script>";
		
		return script;
	}
	
	public int getCurpage() {
		return curpage;
	}
	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}

	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}

	public int getPages() {
		return pages;
	}
	public void setPages(int pages) {
		this.pages = pages;
	}

	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	public String getParam() {
		return param;
	}
	public void setParam(String param) {
		this.param = param;
	}
	
	public int getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	
}